package zcw.com.lib_leet_code;

import java.util.Arrays;
import java.util.List;

import zcw.com.lib_leet_code.bean.TreeNode;
import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/7/15.<br><br>
 *
 * 左叶子之和
 */
public class Topic404 {

    public static void main(String[] args) {
        Topic404 instance = new Topic404();

        List<Integer> list = Arrays.asList(3,9,20,null,null,15,7);
        TreeNode root = Util.createTree(list);
        System.out.println(instance.sumOfLeftLeaves(root));

        list = Arrays.asList(1);
        root = Util.createTree(list);
        System.out.println(instance.sumOfLeftLeaves(root));
    }

    public int sumOfLeftLeaves(TreeNode root) {
        return sumOfLeftLeaves(root, false);
    }

    public int sumOfLeftLeaves(TreeNode root, boolean isLeft) {
        if(root == null) {
            return 0;
        }

        if(isLeft && root.left == null && root.right == null) {
            return root.val;
        }

        return sumOfLeftLeaves(root.left, true) + sumOfLeftLeaves(root.right, false);
    }
}
